package com.chonor.mall.service.impl;

import com.chonor.mall.dao.FlashPromotionProductDao;
import com.chonor.mall.mapper.FlashPromotionProductMapper;
import com.chonor.mall.model.FlashPromotionProduct;
import com.chonor.mall.model.FlashPromotionProductExample;
import com.chonor.mall.service.FlashPromotionProductService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FlashPromotionProductServiceImpl implements FlashPromotionProductService {

    @Autowired
    private FlashPromotionProductMapper flashPromotionProductMapper;

    @Autowired
    private FlashPromotionProductDao flashPromotionProductDao;

    @Override
    public int create(List<FlashPromotionProduct> relationList) {
        for (FlashPromotionProduct relation : relationList) {
            flashPromotionProductMapper.insert(relation);
        }
        return relationList.size();
    }

    @Override
    public int update(Long id, FlashPromotionProduct relation) {
        relation.setId(id);
        return flashPromotionProductMapper.updateByPrimaryKey(relation);
    }

    @Override
    public int delete(Long id) {
        return flashPromotionProductMapper.deleteByPrimaryKey(id);
    }

    @Override
    public FlashPromotionProduct getItem(Long id) {
        return flashPromotionProductMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<FlashPromotionProduct> list(Long flashPromotionId, Long flashPromotionSessionId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        return flashPromotionProductDao.getList(flashPromotionId,flashPromotionSessionId);
    }

    @Override
    public long getCount(Long flashPromotionId, Long flashPromotionSessionId) {
        FlashPromotionProductExample example = new FlashPromotionProductExample();
        example.createCriteria()
                .andFlashPromotionIdEqualTo(flashPromotionId);
        return flashPromotionProductMapper.countByExample(example);
    }
}
